
package it.ibs;

import it.enrollement.System_Param;
import it.utils.Hash;
import jpair.api.FieldElement;
import jpair.pairing.BigInt;
import jpair.pairing.Point;

/**
 * ID-based signatures schemes which is derived from the BLS scheme.
 * 
 * [BONEH2004SHORT] Boneh, Dan and Lynn, Ben and Shacham, Hovav. Short Signatures from the Weil pairing. Springer-Verlag New York, Inc.. p. 297--319 2004 http://dx.doi.org/10.1007/s00145-004-0314-9
 *   
 * 
 * @author Francesco Rossi
 * @version 0.1
 * 
 */
public class IB_BLS_MNT {

    System_Param parameter;
    BigInt modf;
    
     
    public IB_BLS_MNT(System_Param param) {

        parameter = param;
        modf = parameter.getE().getCurve().getField().getOrder();
    }

    public IB_BLS_MNT() {
    }

    /**
     *
     * @param message The message to sign
     * @param SignatureKey The user signature private key
     * @return The message signature
     */
    public Point sign(String message, BigInt kbls) {

        BigInt h1m;
        Point h1p, signature = null;
        Hash h = new Hash();
        try {
            // H1(m)
            h1m = h.h1(message.getBytes("UTF-8"), parameter.getE().getCurve().getField());
            
     
            // sigma = srh(ID)H1(m)P   
            signature = parameter.getE().getCurve().multiply(parameter.getP(),kbls.multiply(h1m));
          
             
            
        } catch (Exception c) {
            c.printStackTrace();

        }

        return signature;

    }

    /**
     *
     * @param message the message
     * @param identity identity of signer
     * @param signature the signature of message
     * @return true if signature is verified
     */
    public boolean vrf(String message, String identity, Point signature) {
        boolean res = false;
        BigInt h1m, h1ide;
        Point A,B;
        FieldElement pair1, pair2;
        Hash h = new Hash();
        try {

            // h1(m)
            h1m = h.h1(message.getBytes("UTF-8"), parameter.getE().getCurve().getField());

            // h1(IDe)   
            h1ide = h.h1(identity.getBytes("UTF-8"), parameter.getE().getCurve().getField());
            
            // srh(ID)H1(m)P  
            A= signature;
            
            // P2
            B= parameter.getP2();
            
            System.out.println(" System to string=" +  A.toString());
              System.out.println(" System to string=" +  B.toString());
            
            //e(sigma,P2)
           // pair1 = parameter.getE().compute(A,h.h2(B.toString().getBytes(),parameter.getE().getCurve()));
            pair1 = parameter.getE().compute(A,B);
           System.out.println(" System to string=" +  pair1.toString());
            //H1(m)rP
            A= parameter.getE().getCurve().multiply(parameter.getW(), h1m);
           
              // h1(ID)sP2
              B= parameter.getE().getCurve2().multiply(parameter.getPpub2(), h1ide);
            
              System.out.println(" System to string=" +  A.toString());
              System.out.println(" System to string=" +  B.toString());
            //e(h1(m)W,h1(ID)Ppub2) 
         //   pair2 = parameter.getE().compute(A,h.h2(B.toString().getBytes(),parameter.getE().getCurve()));
              
                 pair2 = parameter.getE().compute(A,B);
  System.out.println(" System to string=" +  pair2.toString());
            //  System.out.println(pair1.toString());
            //     System.out.println(pair2.toString());

           if (pair1.equals(pair2)) {
                res = true;
              System.out.println("Signature Verification = True");

            } else {
              System.out.println("Signature Verification = False");
                res = false;
            }
            
             

        } catch (Exception c) {
            c.printStackTrace();

        }

        //    System.out.println("Signature Verification for User "+identity+" ="+res);   

        return res;

    }
}
